using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GameForKids
{
    class Fish
    {
        public Vector2 CenterPosition;
        public bool Living;
        public Sprite FishPrite;
        public String TextureLink;
        public Vector2 Velocity;
        public int Direction = 1;
        public int Live = 1;
        public bool Disappear = false;
        private List<int> touchs = new List<int>();

        public Fish(Vector2 centerPosition, Vector2 velocity, int live
                    , int spriteHeight, int spriteWidth
                    , int column, int row, int count, int actionCount)
        {

            FishPrite = new Sprite(spriteHeight, spriteWidth, column, row, count, 0, actionCount, "FishMove", 1.0f / 12.0f);
            Reset(centerPosition, velocity, live);
        }

        public virtual void Reset(Vector2 centerPosition, Vector2 velocity, int live)
        {
            CenterPosition = centerPosition;
            if (CenterPosition.X > 450)
            {
                Direction = -1;
                FishPrite._SpriteEffects = SpriteEffects.None;
            }
            else
            {
                Direction = 1;
                FishPrite._SpriteEffects = SpriteEffects.FlipHorizontally;
            }
            FishPrite.ChangeAction("FishMove");
            Velocity = velocity;
            Living = true;
            Live = live;
            Disappear = false;
            FishPrite._A = 1.0f;
            FishPrite._R = 255.0f;
            FishPrite._G = 255.0f;
            FishPrite._B = 255.0f;
            FishPrite._Color = new Color(FishPrite._R, FishPrite._G, FishPrite._B, FishPrite._A);
            touchs.Clear();
        }

        public bool CheckDie()
        {
            if (Direction == -1)
            {
                if (CenterPosition.X + FishPrite._SpriteWidth  < 0)
                {
                    Living = false;
                    return true;
                }
            }
            else
            {
                if (CenterPosition.X - FishPrite._SpriteWidth > 800)
                {
                    Living = false;
                    return true;
                }
            }
            return false;
        }

        public virtual void DoOnLiveDown()
        {
            Live--;
            if (Live <= 0)
            {
                Living = false;
                Disappear = true;
            }

            if (Live % 2 == 1)
            {
                Direction *= -1;
                if (Direction == 1)
                {
                    FishPrite._SpriteEffects = SpriteEffects.FlipHorizontally;
                }
                else
                {
                    FishPrite._SpriteEffects = SpriteEffects.None;
                }
            }
        }

        public virtual void UpdateFish(GameTime gameTime)
        {
            if (Living == true)
            {
                CenterPosition += Velocity * Direction;
                CheckDie();
            }

            if (Disappear == true)
            {
                FishPrite._A *= 0.8f;
                FishPrite._R = 0;// FishPrite._A * 255;
                FishPrite._G = 0;//FishPrite._A * 255;
                FishPrite._B = 0;//FishPrite._A * 255;
                FishPrite._Color = new Color(FishPrite._R, FishPrite._G, FishPrite._B, FishPrite._A);
                if (FishPrite._A <= 0.05f)
                {
                    Disappear = false;
                }
            }
        }
        public virtual void DrawFish(SpriteBatch batch) 
        {
            if (Living == true || Disappear == true)
            {
                batch.Draw(Suport.GetTexture(TextureLink), FishPrite.ScreenRectangle(CenterPosition), FishPrite._SourceRectangle, FishPrite._Color, FishPrite._Rotation, FishPrite._Original, FishPrite._SpriteEffects, FishPrite._LayerDepth);
            }
        }

        public void Update(GameTime gameTime) { UpdateFish(gameTime); }
        public void Draw(SpriteBatch batch) { DrawFish(batch); }

        public virtual bool IsHit(TouchLocation touch)
        {
            if (touchs.Contains(touch.Id) == true)
            {
                return false;
            }
            else
            {
                Rectangle rec = FishPrite.RealScreenRectangle(CenterPosition);
                if (rec.X <= touch.Position.X && touch.Position.X <= (rec.X + rec.Width) && rec.Y <= touch.Position.Y && touch.Position.Y <= (rec.Y + rec.Height))
                {
                    touchs.Add(touch.Id);
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }
}
